package com.bang.study.day01.stack;

import com.bang.study.day01.list.LinkedList;

/**
 * @Auther: Bang
 * @Date: 2019/9/10 0010 11:48
 * @Description: 基于链表实现的栈 (LIFO) 后进先出的结构
 */
public class LinkedListStack<E> implements Stack<E> {

    private LinkedList<E> list;


    public LinkedListStack() {
        list = new LinkedList<>();
    }

    /**
     * 获取栈的大小
     *
     * @return
     */
    @Override
    public int getSize() {
        return list.getSize();
    }

    /**
     * 是否为空
     *
     * @return
     */
    @Override
    public boolean isEmpty() {
        return list.isEmpty();
    }

    /**
     * 入栈
     *
     * @param e
     */
    @Override
    public void push(E e) {
        list.addFirst(e);
    }

    /**
     * 出栈
     *
     * @return
     */
    @Override
    public E pop() {
        return list.removeFirst();
    }

    /**
     * 查看栈顶元素
     *
     * @return
     */
    @Override
    public E peek() {
        return list.getFirst();
    }

    @Override
    public String toString() {
        return "LinkedListStack{" +
                "list=top[" + list.toString() +"]"+
                '}';
    }

    public static void main(String[] args) {
        LinkedListStack<Integer> listStack = new LinkedListStack<>();
        for (int i = 0; i < 10; i++) {
            listStack.push(i);
            System.out.println(listStack);
        }
        System.out.println("出栈==============================");
        while (!listStack.isEmpty()){
            listStack.pop();
            System.out.println(listStack);
        }

    }
}
